package chord.finder;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.rmi.transport.proxy.CGIHandler;

import com.juitar.base.Chord;
import com.juitar.base.Fingering;
import com.juitar.base.Note;
import com.juitar.base.Tuning;
import com.juitar.base.fingering.IFingeringBuilder;
import com.juitar.base.fingering.RecursiveFingeringBuilder;
import com.juitar.base.utils.Config;

/**
 * Servlet implementation class for Servlet: Controller
 */
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    /**
     * 
     */
    private static final long serialVersionUID = -8139461667187130214L;

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        processRequest(request);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/SearchResult.jsp");
        dispatcher.forward(request, response);
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        throw new ServletException("POST is not supported");
    }

    private void processRequest(HttpServletRequest request) {
        request.setAttribute("notes", Note.NOTES);
        List<Chord> chords = Config.getChords();
        request.setAttribute("chords", chords);
        
        String chordParam = request.getParameter("chord");
        String rootParam = request.getParameter("root");

        
        
        if (rootParam != null) {

            //Chord chord = Chord.create(chordParam);
            int note = Integer.valueOf(rootParam).intValue();
            //chord = new Chord(chord, note);

            int chordIndex = Integer.valueOf(chordParam).intValue();
            Chord c = chords.get(chordIndex);
            Chord chord = new Chord(c, note);
            
            
            Tuning tuning = (Tuning) request.getSession().getAttribute("tuning");
            if (tuning == null) {
                tuning = Tuning.STANDARD;
            }

            String chordName = chord.getChordName();
            request.setAttribute("chordName", chordName);

            IFingeringBuilder builder = new RecursiveFingeringBuilder();
            Collection<Fingering> fingerings = builder.createFingerings(chord, tuning);
            request.setAttribute("fingerings", fingerings);
        }
    }
}